from wordcloud import WordCloud as WC
from back_end.hsa.data_display import keywords_reader

class WordCloud:
    __days_mapping = {'日': 1, '周': 7, '月': 30, '年': 365}

    @staticmethod
    def plot(time_range):
        frequencies = keywords_reader.get_recent_frequencies(WordCloud.__days_mapping[time_range])
        return WordCloud.__get_word_cloud(frequencies if frequencies else {'无数据': 1})

    @staticmethod
    def __get_word_cloud(frequencies):
        return WC(
            font_path='C:/Windows/Fonts/SimHei.ttf',
            background_color='white',
            colormap='tab10',
            width=700,
            height=400
        ).generate_from_frequencies(frequencies).to_svg()
